package com.reservation.controller;

import com.reservation.entity.Reservation;
import com.reservation.service.ReservationService;
import com.Venue.utils.ResponseResult;
import io.github.resilience4j.circuitbreaker.annotation.CircuitBreaker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Collections;
import java.util.List;

@RestController
@CrossOrigin
@RequestMapping(value = "/api/reservation")
public class ReservationController {

    @Autowired
    private ReservationService reservationService;

    /**
     * 获取预订信息
     * @param rid 预订 ID
     * @throws IOException
     */
    @RequestMapping(value = "/get.do", produces = "application/json; charset=utf-8")
    @ResponseBody
    @CircuitBreaker(name = "reservationService", fallbackMethod = "fallbackGetReservationInfo")
    public ResponseResult getReservationInfo(@RequestParam Integer rid) throws IOException {
        // 获取预订信息
        Reservation reservation = reservationService.getReservationById(rid);
        // 返回数据
        return ResponseResult.build(reservation != null, null, reservation);
    }

    /**
     * 获取预订信息失败时的降级方法
     */
    public ResponseResult fallbackGetReservationInfo(@RequestParam Integer rid, Throwable throwable) {
        System.err.println("断路器触发 - 获取预订信息失败: " + throwable.getMessage());
        Reservation defaultReservation = new Reservation();
        defaultReservation.setRid(-1); // 假设设置一个默认的无效 ID
        return ResponseResult.build(false, "系统繁忙，返回临时数据", defaultReservation);
    }

    /**
     * 添加预订信息
     * @param reservation 预订信息
     * @throws IOException
     */
    @RequestMapping(value = "/add.do", produces = "application/json; charset=utf-8")
    @CircuitBreaker(name = "reservationService", fallbackMethod = "fallbackAddReservationInfo")
    public ResponseResult addReservationInfo(@RequestBody Reservation reservation) throws IOException {
        // 添加预订信息
        if (reservationService.insertReservation(reservation)) {
            // 添加预订信息成功
            return ResponseResult.build(true, "添加预订信息成功！");
        } else {
            // 添加预订信息失败
            return ResponseResult.build(false, "添加预订信息失败！");
        }
    }

    /**
     * 添加预订信息失败时的降级方法
     */
    public ResponseResult fallbackAddReservationInfo(@RequestBody Reservation reservation, Throwable throwable) {
        System.err.println("断路器触发 - 添加预订信息失败: " + throwable.getMessage());
        return ResponseResult.build(false, "系统繁忙，请稍后再试");
    }

    /**
     * 修改预订信息
     * @param reservation 预订信息
     * @throws IOException
     */
    @RequestMapping(value = "/modify.do", produces = "application/json; charset=utf-8")
    @ResponseBody
    @CircuitBreaker(name = "reservationService", fallbackMethod = "fallbackModifyReservationInfo")
    public ResponseResult modifyReservationInfo(@RequestBody Reservation reservation) throws IOException {
        // 修改预订信息
        if (reservationService.updateReservation(reservation)) {
            // 修改预订信息成功
            return ResponseResult.success("修改预订信息成功！");
        } else {
            // 修改预订信息失败
            return ResponseResult.failure("修改预订信息失败！");
        }
    }

    /**
     * 修改预订信息失败时的降级方法
     */
    public ResponseResult fallbackModifyReservationInfo(@RequestBody Reservation reservation, Throwable throwable) {
        System.err.println("断路器触发 - 修改预订信息失败: " + throwable.getMessage());
        return ResponseResult.build(false, "系统繁忙，请稍后再试");
    }

    /**
     * 删除预订信息
     * @param rid 预订 ID
     * @throws IOException
     */
    @RequestMapping(value = "/delete.do", produces = "application/json; charset=utf-8")
    @ResponseBody
    @CircuitBreaker(name = "reservationService", fallbackMethod = "fallbackDeleteReservationInfo")
    public ResponseResult deleteReservationInfo(@RequestParam Integer rid) throws IOException {
        // 删除预订信息
        if (reservationService.deleteReservation(rid)) {
            // 删除预订信息成功
            return ResponseResult.build(true, "删除预订信息成功！");
        } else {
            // 删除预订信息失败
            return ResponseResult.build(false, "删除预订信息失败！");
        }
    }

    /**
     * 删除预订信息失败时的降级方法
     */
    public ResponseResult fallbackDeleteReservationInfo(@RequestParam Integer rid, Throwable throwable) {
        System.err.println("断路器触发 - 删除预订信息失败: " + throwable.getMessage());
        return ResponseResult.build(false, "系统繁忙，请稍后再试");
    }

    /**
     * 获取所有预订信息
     */
    @RequestMapping(value = "/list.do", produces = "application/json; charset=utf-8")
    @ResponseBody
    @CircuitBreaker(name = "reservationService", fallbackMethod = "fallbackGetAllReservations")
    public ResponseResult getAllReservations() throws IOException {
        // 获取所有预订信息
        List<Reservation> reservationList = reservationService.getAllReservations();
        // 返回数据（若为空则返回空列表，避免null）
        return ResponseResult.build(true, "获取成功",
                reservationList != null ? reservationList : Collections.emptyList());
    }

    /**
     * 获取所有预订信息失败时的降级方法
     */
    public ResponseResult fallbackGetAllReservations(Throwable throwable) {
        System.err.println("断路器触发 - 获取所有预订信息失败: " + throwable.getMessage());
        // 返回空列表及错误提示
        return ResponseResult.build(false, "系统繁忙，暂无法获取数据", Collections.emptyList());
    }
}